<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>
<?php 
	$ci = & get_instance();
	if(!isUserLogin()){
		redirect(site_url());exit();
	}
	$USER = $ci->user_entity->init('id', getUserId());
	$email_checked = $USER->getEmailSubscription() == 'yes' ? 'checked="checked"':'';
	$sms_checked = $USER->getSmsSubscription() == 'yes' ? 'checked="checked"':'';
	
?>
<form action="<?php echo site_url('cricket/account/submit_subscription');?>" name="submit_subscription" id="submit_subscription" method="post" enctype="multipart/form-data" >
<div id="center_content">
	<div class="clrboth"></div>
	<div class="block">
		<div class="title" style="position:relative;">
			<h3 class="bold"><a href="<?php echo site_url("cricket/user/dashboard")?>">Dashboard</a> &raquo; Subscriptions</h3>
			<a class="back_button2" href="<?php echo site_url("cricket/user/dashboard")?>" title="Back?">Back?</a>	
		</div>
		<div class="clrboth"></div>
		<div class="content">
			<div class="rowitem3">
				<div class="clrboth"></div>
				
				<div class="grid8">
					<input type="checkbox" name="email_subscription" id="email_subscription" value="yes" <?php echo $email_checked;?> /> 
					I want to receive email subscription.
				</div>
				<div class="clrboth"></div>
				
				<div class="grid8">
					<input type="checkbox" name="sms_subscription" id="sms_subscription" value="yes" <?php echo $sms_checked;?> /> 
					I want to receive SMS subscription.
				</div>
				<div class="clrboth"></div>
				
				<div class="grid8" style="margin-top:15px;"><input type="submit" class="submitclass" /> <?php echo loader_image('id="loader_image" class="hidden"');?></div>
				<div class="grid2"><span class="bold red hidden" id="submit_error">Missing.</span></div>	
				<?php echo form_hidden( "user_id", $USER->getId() ); ?>
				<div class="clrboth"></div>	
				
				<div class="grid8" id="system_error"></div>

				<div class="clrboth"></div>		
			</div>	
			<div class="clrboth"></div>
		</div>
	</div>	
	<div class="clrboth"></div>
</div>	
</form>

<script type="text/javascript">
	google.setOnLoadCallback(function() {
		var option = { 
			beforeSubmit:  validateBeforeSubmit,  
			success: processAfterSubmit
		};	
		$('#submit_subscription').ajaxForm(option);
	});
	
	function validateBeforeSubmit(formData, jqForm, options){
		$('#loader_image').show();
		return true;
	}
	
	function processAfterSubmit(responseText, statusText, xhr, $form){
		$('#loader_image').hide();
		var p = new PHP_JS();
		var result_arr = p.explode('#',responseText); 
		if(result_arr[0] == 'msg'){
			$('#system_error').html(result_arr[1]).show();
			
			var redirect = BASE_URI+MODULE+"/user/dashboard";
			window.setTimeout(function(){queryURL(redirect )},4000);
		}else if(result_arr[0] == 'err'){
			for(i in result_arr){
				$('#'+result_arr[i]).show();
			}
		}
	}
</script>